package com.cby.leetcode.滑动窗口.LC713;

/**
 * ClassName: LC712
 * Package: com.cby.leetcode.滑动窗口.LC713
 * Description:
 *
 * @Author dhu-bion
 * @Create 2025/9/10 16:05
 * @Version 1.0
 */
public class LC713 {
    public static void main(String[] args){

    }

    static class Solution {
        public int numSubarrayProductLessThanK(int[] nums, int k) {
            //前提：对于固定的右端点right, 若left到right乘积 < k, 则r - l + 1均 < k

            //特判
            if (k <= 1) {
                return 0;
            }

            int left = 0;
            int total = 1;
            int res = 0;

            //1.枚举右端点
            for (int right = 0; right < nums.length; right++) {
                total *= nums[right];
                while(total >= k){
                    //当前已超
                    total /= nums[left];
                    left ++;    // 缩小窗口
                }

                // 对于固定的 right，有 right-left+1 个合法的左端点
                res += right - left + 1;
            }

            return res;
        }
    }

}
